package com.tx.admin.detailmodehead.controller;

import com.tx.admin.characterfetter.pojo.po.CharacterFetter;
import com.tx.admin.detailmodehead.pojo.dto.CharacterHeadFetterListDTO;
import com.tx.admin.detailmodehead.pojo.po.DetailModeHead;
import com.tx.admin.detailmodehead.pojo.vo.DetailModeHeadActorVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import lombok.RequiredArgsConstructor;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import com.tx.core.common.entity.ApiPageResult;
import com.tx.core.common.entity.ApiResult;
import com.tx.core.common.constant.GlobalConstant;
import com.tx.core.common.entity.PageResult;
import com.tx.core.common.entity.SelectIdsDTO;
import com.tx.admin.detailmodehead.service.DetailModeHeadService;
import com.tx.admin.detailmodehead.pojo.dto.DetailModeHeadCreateDTO;
import com.tx.admin.detailmodehead.pojo.dto.DetailModeHeadUpdateDTO;
import com.tx.admin.detailmodehead.pojo.dto.DetailModeHeadListDTO;
import com.tx.admin.detailmodehead.pojo.vo.DetailModeHeadVO;
import com.tx.core.common.entity.ImportExcelDTO;
import jakarta.servlet.http.HttpServletResponse;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 详情页模板头部 Controller
 * </p>
 *
 * @author sz-admin
 * @since 2025-08-05
 */
@Tag(name =  "详情页模板头部")
@RestController
@RequestMapping("detail-mode-head")
@RequiredArgsConstructor
public class DetailModeHeadController  {

    private final DetailModeHeadService detailModeHeadService;

    @Operation(summary = "新增")
    @SaCheckPermission(value = "detail.mode.head.create")
    @PostMapping
    public ApiResult<Void> create(@RequestBody DetailModeHeadCreateDTO dto) {
        detailModeHeadService.create(dto);
        return ApiResult.success();
    }

    @Operation(summary = "修改")
    @SaCheckPermission(value = "detail.mode.head.update")
    @PutMapping
    public ApiResult<Void> update(@RequestBody DetailModeHeadUpdateDTO dto) {
        detailModeHeadService.update(dto);
        return ApiResult.success();
    }

    @Operation(summary = "删除")
    @SaCheckPermission(value = "detail.mode.head.remove")
    @DeleteMapping
    public ApiResult<Void> remove(@RequestBody SelectIdsDTO dto) {
        detailModeHeadService.remove(dto);
        return ApiResult.success();
    }

    @Operation(summary = "列表查询")
    @SaCheckPermission(value = "detail.mode.head.query_table")
    @GetMapping
    public ApiResult<PageResult<DetailModeHeadVO>> list(DetailModeHeadListDTO dto) {
        return ApiPageResult.success(detailModeHeadService.page(dto));
    }

    /**
     *
     * 为兼容组件弄出的连表分页查询
     * @param dto
     * @return
     */
    @Operation(summary = "关系模式查询")
    @PostMapping("fetters")
    public ApiResult<PageResult<DetailModeHeadActorVO>> listWithFetter(@RequestBody CharacterHeadFetterListDTO dto) {
        DetailModeHeadListDTO dtoT = new DetailModeHeadListDTO();
        BeanUtils.copyProperties(dto,dtoT);
        PageResult<DetailModeHeadVO> page = detailModeHeadService.page(dtoT);
        List<DetailModeHeadActorVO> newVos = page.getRows().stream().map(s -> {
            DetailModeHeadActorVO vo = new DetailModeHeadActorVO();
            BeanUtils.copyProperties(s, vo);
            vo.setId(s.getId());
            vo.setBelongId(dto.getBelongId());
            vo.setType(dto.getType());
            vo.setTargetId(s.getId());
            vo.setRelateDes(dto.getRelateDes());
            return vo;
        }).collect(Collectors.toList());

        PageResult<DetailModeHeadActorVO> actorPage
                = new PageResult<>(page.getCurrent(), page.getLimit(), page.getTotalPage(),page.getTotal(),newVos);
        return ApiResult.success(actorPage);
    }

    @Operation(summary = "详情")
    @SaCheckPermission(value = "detail.mode.head.query_table")
    @GetMapping("/{id}")
    public ApiResult<DetailModeHeadVO> detail(@PathVariable Object id) {
        return ApiResult.success(detailModeHeadService.detail(id));
    }

    @Operation(summary = "导入")
    @Parameters({
      @Parameter(name = "file", description = "上传文件", schema = @Schema(type = "string", format = "binary"), required = true),
    })
    @SaCheckPermission(value = "detail.mode.head.import")
    @PostMapping("/import")
    public void importExcel(@ModelAttribute ImportExcelDTO dto) {
        detailModeHeadService.importExcel(dto);
    }

    @Operation(summary = "导出")
    @SaCheckPermission(value = "detail.mode.head.export")
    @PostMapping("/export")
    public void exportExcel(@RequestBody DetailModeHeadListDTO dto, HttpServletResponse response) {
        detailModeHeadService.exportExcel(dto, response);
    }
}